
package com.thxrinfo.operate.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thxrinfo.operate.api.entity.Merchant;
import com.thxrinfo.operate.api.entity.MerchantChangeLog;
import com.thxrinfo.operate.api.entity.MerchantRechargeLog;
import com.thxrinfo.operate.mapper.MerchantChangeLogMapper;
import com.thxrinfo.operate.mapper.MerchantRechargeLogMapper;
import com.thxrinfo.operate.service.MerchantChangeLogService;
import com.thxrinfo.operate.service.MerchantRechargeLogService;
import com.thxrinfo.operate.service.MerchantService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * 商户充值记录表
 *
 * @author ct
 * @date 2023-03-09 14:02:13
 */
@Service
@RequiredArgsConstructor
public class MerchantRechargeLogServiceImpl extends ServiceImpl<MerchantRechargeLogMapper, MerchantRechargeLog> implements MerchantRechargeLogService {
	private final MerchantService merchantService;

	private final MerchantChangeLogService merchantChangeLogService;

	private final MerchantRechargeLogMapper merchantRechargeLogMapper;

	@Override
	@Transactional (rollbackFor = Exception.class)
	public boolean save(MerchantRechargeLog merchantRechargeLog) {
		//更新商户余额 = 当前余额 + 充值金额 + 赠送金额
		Long merchantId = merchantRechargeLog.getMerchantId();
		Merchant merchant = merchantService.getById(merchantId);
		Merchant updateObj = new Merchant();
		updateObj.setId(merchantId);
		BigDecimal newBalance = merchant.getBalance().
				add(merchantRechargeLog.getRechargeMoney()).
				add(merchantRechargeLog.getGiveMoney());
		updateObj.setBalance(newBalance);
		merchantService.updateById(updateObj);
		//插入余额变更记录
		saveMerchantRechargeLog(merchantRechargeLog);

		return baseMapper.insert(merchantRechargeLog) == 1 ? Boolean.TRUE : Boolean.FALSE;
	}

	@Override
	public IPage<MerchantRechargeLog> page(Page page, MerchantRechargeLog merchantRechargeLog) {
		return merchantRechargeLogMapper.page(page, merchantRechargeLog);
	}


	/**
	 * 插入余额变更记录
	 * @param merchantRechargeLog
	 */
	private void saveMerchantRechargeLog(MerchantRechargeLog merchantRechargeLog) {
		MerchantChangeLog merchantChangeLog = new MerchantChangeLog();
		merchantChangeLog.setMerchantId(merchantRechargeLog.getMerchantId());
		merchantChangeLog.setChangeMoney(merchantRechargeLog.getRechargeMoney());
		merchantChangeLog.setChangeType("1");
		merchantChangeLog.setReason("商户余额充值");
		merchantChangeLog.setReasonType("1");
		merchantChangeLog.setRemarks("商户余额充值,余额变更记录");
		merchantChangeLogService.save(merchantChangeLog);

		MerchantChangeLog merchantChangeGiveLog = new MerchantChangeLog();
		merchantChangeGiveLog.setMerchantId(merchantRechargeLog.getMerchantId());
		merchantChangeGiveLog.setChangeMoney(merchantRechargeLog.getRechargeMoney());
		merchantChangeGiveLog.setChangeType("2");
		merchantChangeGiveLog.setReason("商户余额赠送");
		merchantChangeGiveLog.setReasonType("2");
		merchantChangeGiveLog.setRemarks("商户余额赠送,余额变更记录");
		merchantChangeLogService.save(merchantChangeGiveLog);
	}


}
